﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_Amount_RechargeController : UserController
    {
        public IUser_rechargeService _user_recharge { get; set; }
        public ISite_paymentService _site_payment { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(decimal amount, int payment_id)
        {
            //检查用户是否登录
            Users model = GetUserInfo();
            if (model == null)
            {
                return Ok(new { status=0,msg= "用户尚未登录或已超时！" });
            }
            if (amount == 0)
            {
                return Ok(new { status = 0, msg = "请输入正确的充值金额！" });
            }
            if (payment_id == 0)
            {
                return Ok(new { status = 0, msg = "请选择正确的支付方式！" });
            }
            if (!_site_payment.Exists(payment_id))
            {
                return Ok(new { status = 0, msg = "支付方式不存在或已删除！" });
            }
            payment_id = _site_payment.GetModel(payment_id).payment_id;
            //生成订单号
            string order_no = "R" + Utils.GetOrderNumber(); //订单号R开头为充值订单
            _user_recharge.Add(model.id, model.user_name, order_no, payment_id, amount);
            //保存成功后返回订单号
            return Ok(new { status = 1, msg = "订单保存成功！", order_no= order_no });
        }
    }
}